import json
from constr_budgets.models import Templates, Place, Modules
from django.http import JsonResponse
from django.views import View


# 模板接口
class Template_port(View):
    def get(self, request):
        keyword = request.GET.get('keyword')
        pagenum = request.GET.get('pagenum')
        pagesize = request.GET.get('pagesize')
        # print(keyword)
        if pagenum is None or pagesize is None:

            data = Templates.objects.all().values()
            c = Templates.objects.count()
            return JsonResponse({
                'code': 200,
                'data': list(data),
                "count": c,
            })
        else:
            e = (int(pagenum) - 1) * int(pagesize)
            h = int(pagenum) * int(pagesize)
            if keyword is None:
                data = Templates.objects.all().values()
                data = data[e:h]
            else:
                print(keyword)
                data = Templates.objects.filter(name__icontains=keyword).values()
                data = data[e:h]

            # alldata = Templates.objects.all().values()
            c = Templates.objects.count()
            return JsonResponse({
                'code': 200,
                'count': c,
                'data': list(data),

            })

    def post(self, request):
        data = json.loads(request.body)  # 获取post 上传的添加模板信息
        print(data)
        Templates.objects.create(name=data['name'])  # 添加模板到数据库
        return JsonResponse({
            'code': 200,
            'message': '添加模板成功',
        })

    def put(self, request):
        data = json.loads(request.body)
        Templates.objects.filter(id=data['id']).update(name=data['name'])
        return JsonResponse({
            'code': 200,
            "message": "修改模板信息成功"
        })

    def delete(self, request):
        d = request.GET.get("id")
        print(d)
        Templates.objects.filter(id=d).delete()
        return JsonResponse({
            'code': 200,
            "message": "删除模板信息成功"
        })

    def patch(self, req):
        d = json.loads(req.body)
        names = d["name"] + "(复制版)"
        cc = Templates.objects.create(name=names)
        places = list(Place.objects.filter(template=d["id"]).values())
        mudels = list(Modules.objects.filter(template=d["id"]).values())
        for i in places:
            i["template_id"] = cc.id
            Place.objects.create(name=i["name"], square=i["square"], total_money=i["total_money"],
                                 one_total_money=i["one_total_money"], template_id=i["template_id"])
        old = []
        old_p = []
        new = []
        new_p = []
        new_i = []
        for i in mudels:
            i["template_id"] = cc.id
            old.append(i["id"])
            if i["pid"] != 0:
                old_p.append(i["pid"])
            aa = Modules.objects.create(name=i["name"], level=i["level"], pid=i["pid"],
                                        predict_money=i["predict_money"],
                                        total_money=i["total_money"], total_do=i["total_do"],
                                        over_money=i["over_money"],
                                        over_do=i["over_do"], one_predict_money=i["one_predict_money"],
                                        one_predict_do=i["one_predict_do"],
                                        one_total_money=i["one_total_money"], one_total_do=i["one_total_do"],
                                        one_over_money=i["one_over_money"], one_over_do=i["one_over_do"],
                                        template_id=i["template_id"],
                                        predict_do=i["predict_do"], unit=i["unit"], is_materials=i["is_materials"],
                                        status=i["status"])
            new.append(aa.id)
            if aa.pid != 0:
                new_p.append(aa.id)
        for i, item in enumerate(old):
            for j in old_p:
                if j == item:
                    new_i.append(i)
        for i in range(0, len(new_p)):

            Modules.objects.filter(id=new_p[i]).update(pid=new[new_i[i]])
        return JsonResponse({"code": 200})
